home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / sysutl / exec2.asm < prev    next >
Assembly Source File  |  1984-05-12  |  6KB  |  269 lines

  1. PAGE 55,132
  2. ;
  3. ; name        exec -- system call for .COM files
  4. ;
  5. ; synopsis    status = exec(cmd);
  6. ;        int status;        error returns
  7. ;         char *cmd;        command to execute
  8. ;
  9. ; description    This function accepts a string with the pathname and
  10. ;        parameters of a command to be executed and executes it.
  11. ;        See Appendix F. of the DOS 2.0 Reference Manual for more
  12. ;        information.
  13. ;
  14. ; returns    0:    Successful
  15. ;        -1:    Insufficient Memory
  16. ;        -2:    Access Denied
  17. ;        -3:    No such command
  18. ;        -4:    Invalid command format
  19. ;        -5:    Memory control blocks destroyed
  20. ;        -6:    Invalid memory block address
  21. ;
  22. ; cautions    Use only with .COM files built with the Lattice C
  23. ;        compiler, linked with CC.OBJ, and translated to
  24. ;        .COM format with EXE2BIN.
  25. ;
  26. ; authors    Written by Darrel Plank.  Modified from the macro by Brad
  27. ;        Davis (b-davis@utah-cs).  The prolog and epilog are modified
  28. ;        from Jim Holtman's macros for Pascal.  Fixed by Marco Papa
  29. ;        (papa.use-cse@csnet-relay).
  30. ;
  31.  
  32. PROLOG    MACRO
  33.     PUSH    BP
  34.     MOV    BP,SP
  35.     ENDM
  36.  
  37. EPILOG    MACRO    NUM
  38.     POP    BP
  39.     RET
  40.     ENDM
  41.  
  42. EXECVAL    EQU    0
  43. OVLVAL    EQU    3
  44. FNCINT    EQU    21H
  45. SETBLK    EQU    4AH
  46. EXECF    EQU    4BH
  47. CR    EQU    0DH
  48.  
  49. PSP     STRUC
  50. INTVECT DW      ?
  51. TOM     DW      ?
  52. RES1    DB      ?
  53. DOSLONG DB      5 DUP (?)
  54. TERMINA DD      ?
  55. CTRLBRK DD      ?
  56. CRITERR DD      ?
  57. DOS1    DB      22 DUP (?)
  58. ENVIRO  DW      ?
  59. DOS2    DB      46 DUP (?)
  60. FPA1    DB      16 DUP (?)
  61. FPA2    DB      20 DUP (?)
  62. UPA     DB      128 DUP (?)
  63. PSP     ENDS
  64.  
  65. EXECDEF STRUC
  66. NENVIRO DW
  67. COMMND  DW      2 DUP (0)
  68. FCB5CH  DW      2 DUP (0)
  69. FCB6CH  DW      2 DUP (0)
  70. EXECDEF ENDS
  71.  
  72. PGROUP    GROUP PROG
  73. PROG    SEGMENT BYTE PUBLIC 'prog'
  74.     ASSUME CS:PGROUP
  75.  
  76. PUBLIC    EXEC
  77. EXEC    PROC NEAR
  78.         PROLOG
  79.         PUSH    DS
  80.         PUSH    ES
  81. ;
  82. ; free up as much memory as we can
  83. ;
  84.     MOV    AX,CS
  85.     PUSH    ES        ; Save ES for later
  86.     MOV    ES,AX
  87.     MOV    BX,SS
  88.     SUB    BX,AX
  89.     ADD    BX,1000H    ; 64K for stack segment
  90.     MOV    AH,SETBLK
  91.     INT    FNCINT
  92.     JNC    NEAR PTR LBL1
  93.     POP    ES        ; ****
  94.     JMP    NEAR PTR LBL2    ; ****
  95. LBL1:    POP    ES        ; Get ES's original value
  96. ;
  97. ; Save SS and SP registers
  98. ;
  99.         MOV     CS:SPSAVE,SP
  100.         MOV     CS:SSSAVE,SS
  101. ;
  102. ; set up the parameter block
  103. ;
  104.         MOV     CS:EXECBLK.NENVIRO,0    ; Inherit envir. from parent
  105.     MOV    AX,3700H        ; Undocumented call for SWITCHAR
  106. ;
  107. ; W A R N I N G:  The following function call is undocumented and is
  108. ; liable to disappear or change in future versions of DOS.
  109. ;
  110.     INT    FNCINT
  111.     MOV    CS:COMMAND[1],DL    ; Switchar
  112.     MOV    DX,4[BP]        ; Address of the command
  113.     MOV    SI,DX
  114.     MOV    DI,DX
  115.     CLD
  116.     XOR    AL,AL
  117.     MOV    CX,100H            ; Longest string can be 100h
  118.     REPNE SCASB            ; Find Null termination
  119.     SUB    DX,DI
  120.     NEG    DX
  121.     MOV    CX,DX
  122.     DEC    CX
  123.     ADD    DX,2
  124.     MOV    CS:COMMAND[0],DL    ; Save command length
  125.     LEA    DI,CS:COMMAND[4]
  126.     MOV    AX,CS
  127.     MOV    ES,AX
  128.     REP MOVSB            ; Copy command into our buffer
  129. ASSUME    DS:PGROUP
  130.     MOV    AX,CS
  131.     MOV    DS,AX            ; DS points at code segment
  132.     MOV    BYTE PTR [DI],CR    ; Put in Carriage Return
  133.     LEA    DX,COMMAND
  134.         MOV     EXECBLK.COMMND[0],DX
  135.         MOV     EXECBLK.COMMND[2],DS
  136.     MOV    BX,OFFSET EXECBLK
  137.         XOR     SI,SI
  138.     MOV    DS,[SI].ENVIRO        ; Get environment address
  139. ASSUME    DS:NOTHING
  140.     LEA    SI,CS:COMSPEC        ; Point SI at env. variable name
  141.     PUSH    DS            ; Swap
  142.     PUSH    ES            ; ES
  143.     POP    DS            ; and
  144.     POP    ES            ; DS
  145.     CALL    GETENV
  146.     PUSH    DS            ; Swap
  147.     PUSH    ES            ; them
  148.     POP    DS            ; back
  149.     POP    ES            ; again
  150.         MOV     AH,EXECF
  151.     MOV    AL,EXECVAL        ; OVLVAL here for overlay
  152.         INT     FNCINT
  153.     MOV     SS,CS:SSSAVE        ; ****
  154.         MOV     SP,CS:SPSAVE        ; ****
  155.     JC    NEAR PTR LBL2
  156.     MOV    AX,0            ; Successful exec
  157.     JMP    NEAR PTR FINE
  158. LBL2:    MOV    SI,AX
  159.     MOV    AL,CS:ERRORS[SI]    ; Get error code
  160.     MOV    AH,0FFH            ; Sign extension - Assume Negative
  161. FINE:    POP     ES            ; ****
  162.         POP     DS
  163.     EPILOG    1
  164. EXECBLK EXECDEF <>
  165. ;
  166. ; first byte of command is length excluding the length byte and the
  167. ; trailing \r.  Second byte is switchar.
  168. ;
  169. COMMAND    DB    2 DUP(?),"C ",254 DUP(?)
  170. COMSPEC    DB    "COMSPEC",0
  171. SPSAVE  DW
  172. SSSAVE  DW
  173. ERRORS    DB    ?
  174.     DB    ?
  175.     DB    -3    ; No such command
  176.     DB    ?
  177.     DB    ?
  178.     DB    -2    ; Access denied
  179.     DB    ?
  180.     DB    -5    ; Memory control blocks destroyed
  181.     DB    -1    ; Insufficient memory
  182.     DB    -6    ; Invalid memory block address ****
  183.     DB    ?
  184.     DB    -4    ; Invalid command format
  185.  
  186. EXEC    ENDP
  187.  
  188. ;
  189. ; Getenv expects ES to have the environment paragraph and DS:SI to point
  190. ; to an ASCIIZ string with the desired environment variable in it.
  191. ; It returns the address of the proper string in ES:DX.
  192. ;
  193.  
  194.     PUBLIC GETENV
  195. GETENV    PROC NEAR
  196.  
  197.     PROLOG
  198.     PUSH    AX
  199.     PUSH    CX
  200.     PUSH    SI
  201.     PUSH    DI
  202.     MOV    CS:VARNAME,SI        ; Save offset of env. name
  203.     XOR    DI,DI
  204. ;
  205. ; At this point ds:si points to dummy variable environment name and
  206. ; es:di points to environment.
  207. ;
  208.     CLD                ;Forward string operations
  209. TOP:
  210.     LODSB                ;Get a char. of env. name
  211.     CMP    AL,0            ;If we're at the end
  212.     JNE    NEAR PTR LBL3
  213.     CMP    BYTE PTR ES:[DI],'='    ;Check for match
  214.     JNE    NEAR PTR LBL4
  215. ;
  216. ; We matched
  217. ;
  218.     INC    DI            ;Move beyond '='
  219.     MOV    DX,DI
  220.     POP    DI
  221.     POP    SI
  222.     POP    CX
  223.     POP    AX
  224.     EPILOG    2
  225. LBL4:
  226. ;
  227. ; At this point we found the end of the Env. variable name but it didn't
  228. ; match because the env. string was too long
  229. ;
  230.     MOV    CX,-1
  231.     REPNE SCASB            ;Find the end of the env. string
  232.     CMP    BYTE PTR ES:[DI],0
  233.     JNE    LBL3
  234.     MOV    AX,-1            ;End of environment area
  235.     POP    DI
  236.     POP    SI
  237.     POP    CX
  238.     POP    AX
  239.     EPILOG    2
  240. LBL3:
  241. ;
  242. ; Check if the next character matches
  243. ;
  244.     AND    AX,11011111b        ;Capitalize the character in ax
  245.     SCASB
  246.     JE    TOP
  247. ;
  248. ; If we get here we don't have a match so move on
  249. ;
  250.     MOV    SI,CS:VARNAME        ;Go back to start of env. string
  251.     XOR    AX,AX
  252.     MOV    CX,-1
  253.     REPNE SCASB            ;Go to next env. variable
  254.     CMP    BYTE PTR ES:[DI],0
  255.     JNE    TOP
  256.     MOV    AX,-1            ;End of environment area
  257.     POP    DI
  258.     POP    SI
  259.     POP    CX
  260.     POP    AX
  261.     EPILOG    2
  262.  
  263. VARNAME    DW    ?
  264.  
  265. GETENV    ENDP
  266.  
  267. PROG    ENDS
  268.     END
  269.